<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  

  
  <title>4. Docker的常用命令 | Hexo</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <meta name="description" content="docker  ps  【查看当前运行的】docker  ps -a 【查看停止的容器】docker  restart （容器Id）  【根据容器id重启容器】docker stop 容器id&#x2F;容器名字  【停止容器根据id或者名字】docker rm 容器id 【删除容器 根据容器id】   docker run -p 8080:8080 –name tomcat tomcat 【  启动一个">
<meta property="og:type" content="article">
<meta property="og:title" content="4. Docker的常用命令">
<meta property="og:url" content="http://yoursite.com/2020/02/23/4.Docker%20%E5%B8%B8%E7%94%A8%E5%91%BD%E4%BB%A4/index.html">
<meta property="og:site_name" content="Hexo">
<meta property="og:description" content="docker  ps  【查看当前运行的】docker  ps -a 【查看停止的容器】docker  restart （容器Id）  【根据容器id重启容器】docker stop 容器id&#x2F;容器名字  【停止容器根据id或者名字】docker rm 容器id 【删除容器 根据容器id】   docker run -p 8080:8080 –name tomcat tomcat 【  启动一个">
<meta property="og:locale" content="en_US">
<meta property="article:published_time" content="2020-02-23T07:34:26.847Z">
<meta property="article:modified_time" content="2020-02-28T09:31:16.227Z">
<meta property="article:author" content="John Doe">
<meta name="twitter:card" content="summary">
  
    <link rel="alternate" href="/myblog/atom.xml" title="Hexo" type="application/atom+xml">
  
  
    <link rel="icon" href="/favicon.png">
  
  
    <link href="//fonts.googleapis.com/css?family=Source+Code+Pro" rel="stylesheet" type="text/css">
  
  
<link rel="stylesheet" href="/myblog/css/style.css">

<meta name="generator" content="Hexo 4.2.0"></head>

<body>
  <div id="container">
    <div id="wrap">
      <header id="header">
  <div id="banner"></div>
  <div id="header-outer" class="outer">
    <div id="header-title" class="inner">
      <h1 id="logo-wrap">
        <a href="/myblog/" id="logo">Hexo</a>
      </h1>
      
    </div>
    <div id="header-inner" class="inner">
      <nav id="main-nav">
        <a id="main-nav-toggle" class="nav-icon"></a>
        
          <a class="main-nav-link" href="/myblog/">Home</a>
        
          <a class="main-nav-link" href="/myblog/archives">Archives</a>
        
      </nav>
      <nav id="sub-nav">
        
          <a id="nav-rss-link" class="nav-icon" href="/myblog/atom.xml" title="RSS Feed"></a>
        
        <a id="nav-search-btn" class="nav-icon" title="Search"></a>
      </nav>
      <div id="search-form-wrap">
        <form action="//google.com/search" method="get" accept-charset="UTF-8" class="search-form"><input type="search" name="q" class="search-form-input" placeholder="Search"><button type="submit" class="search-form-submit">&#xF002;</button><input type="hidden" name="sitesearch" value="http://yoursite.com"></form>
      </div>
    </div>
  </div>
</header>
      <div class="outer">
        <section id="main"><article id="post-4.Docker 常用命令" class="article article-type-post" itemscope itemprop="blogPost">
  <div class="article-meta">
    <a href="/myblog/2020/02/23/4.Docker%20%E5%B8%B8%E7%94%A8%E5%91%BD%E4%BB%A4/" class="article-date">
  <time datetime="2020-02-23T07:34:26.847Z" itemprop="datePublished">2020-02-23</time>
</a>
    
  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 class="article-title" itemprop="name">
      4. Docker的常用命令
    </h1>
  

      </header>
    
    <div class="article-entry" itemprop="articleBody">
      
        <blockquote>
<p>docker  ps  <u>【查看当前运行的】</u><br>docker  ps -a <u>【查看停止的容器】</u><br>docker  restart （容器Id）  <u>【根据容器id重启容器】</u><br>docker stop 容器id/容器名字  <u>【停止容器根据id或者名字】</u><br>docker rm 容器id <u>【删除容器 根据容器id】</u>  </p>
<p>docker run -p 8080:8080 –name tomcat tomcat <u>【<br>  启动一个容器名为tomcat的tomcat镜像：第一个tomcat是自定义的容器名，<br>  第二个tomncat是容器的镜像】  </u>  </p>
<p>docker exec -it <u>容器ID bash 【进入容器】</u><br>docker rm 容器id <u>删除容器</u><br>docker container stop 容器id/容器名称 <u>【停止容器】</u><br>docker rm -f 容器id <u>删除容器</u><br>docker container prune  <u>将所有未运行状态的容器全部删除</u></p>
</blockquote>
<h4 id="守护态运行"><a href="#守护态运行" class="headerlink" title="守护态运行"></a>守护态运行</h4><pre><code>docker run -p 8080:8080 --name tomcat -d tomcat  </code></pre><p>让docker的运行在后台运行，不让其占用宿主机的主线程。 主要就是在启动容器的时候，加上<br><strong>-d</strong> 这个标签.让容器在宿主机的后台运行。<br>如上述启动语句，表示的是以守护态运行tomcat镜像。其返回的容器id是完整的id（一般情况<br>下返回的是完整容器id的前12位。）</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">docker run -p <span class="number">8081</span>:<span class="number">8080</span> --name tomcat1 -d tomcat</span><br></pre></td></tr></table></figure>

<h4 id="进入运行状态下的容器内部"><a href="#进入运行状态下的容器内部" class="headerlink" title="进入运行状态下的容器内部"></a>进入运行状态下的容器内部</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">docker exec -it 容器ID bash</span><br></pre></td></tr></table></figure>
<p>当仅使用 -i 的时候，由于没有配置伪终端，界面上面没有我门熟悉的Linux页面，但是命令<br>执行结果仍然可以显示。可是当-i -t一起使用的时候可以看到我们熟悉的linux 提示符。</p>
<h4 id="删除容器"><a href="#删除容器" class="headerlink" title="删除容器"></a>删除容器</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">docker rm -f 容器id</span><br></pre></td></tr></table></figure>
<p>根据容器的id删除容器</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">docker container prune</span><br></pre></td></tr></table></figure>
<p>将所有未运行状态下的容器全部删除</p>

      
    </div>
    <footer class="article-footer">
      <a data-url="http://yoursite.com/2020/02/23/4.Docker%20%E5%B8%B8%E7%94%A8%E5%91%BD%E4%BB%A4/" data-id="ck77i1hi00000rkd16an01jjm" class="article-share-link">Share</a>
      
      
    </footer>
  </div>
  
    
<nav id="article-nav">
  
    <a href="/myblog/2020/02/28/5.Docker%20%E5%AE%B9%E5%99%A8%E6%95%B0%E6%8D%AE%E6%8C%81%E4%B9%85%E5%8C%96/" id="article-nav-newer" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Newer</strong>
      <div class="article-nav-title">
        
          5. Docker容器数据持久化
        
      </div>
    </a>
  
  
    <a href="/myblog/2020/02/10/3.DockerFile%E5%AE%9A%E5%88%B6%E9%95%9C%E5%83%8F/" id="article-nav-older" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Older</strong>
      <div class="article-nav-title">3. DockerFile定制镜像</div>
    </a>
  
</nav>

  
</article>

</section>
        
          <aside id="sidebar">
  
    

  
    

  
    
  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Archives</h3>
    <div class="widget">
      <ul class="archive-list"><li class="archive-list-item"><a class="archive-list-link" href="/myblog/archives/2020/02/">February 2020</a></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Recent Posts</h3>
    <div class="widget">
      <ul>
        
          <li>
            <a href="/myblog/2020/02/29/6.Docker%20%E4%B8%89%E5%89%91%E5%AE%A2/">6. Docker三剑客</a>
          </li>
        
          <li>
            <a href="/myblog/2020/02/28/5.Docker%20%E5%AE%B9%E5%99%A8%E6%95%B0%E6%8D%AE%E6%8C%81%E4%B9%85%E5%8C%96/">5. Docker容器数据持久化</a>
          </li>
        
          <li>
            <a href="/myblog/2020/02/23/4.Docker%20%E5%B8%B8%E7%94%A8%E5%91%BD%E4%BB%A4/">4. Docker的常用命令</a>
          </li>
        
          <li>
            <a href="/myblog/2020/02/10/3.DockerFile%E5%AE%9A%E5%88%B6%E9%95%9C%E5%83%8F/">3. DockerFile定制镜像</a>
          </li>
        
          <li>
            <a href="/myblog/2020/02/05/2.Java%E5%B9%B6%E5%8F%91%E7%BC%96%E7%A8%8B%E7%AC%94%E8%AE%B0/">2. Java并发编程笔记</a>
          </li>
        
      </ul>
    </div>
  </div>

  
</aside>
        
      </div>
      <footer id="footer">
  
  <div class="outer">
    <div id="footer-info" class="inner">
      &copy; 2020 John Doe<br>
      Powered by <a href="http://hexo.io/" target="_blank">Hexo</a>
    </div>
  </div>
</footer>
    </div>
    <nav id="mobile-nav">
  
    <a href="/myblog/" class="mobile-nav-link">Home</a>
  
    <a href="/myblog/archives" class="mobile-nav-link">Archives</a>
  
</nav>
    

<script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>


  
<link rel="stylesheet" href="/myblog/fancybox/jquery.fancybox.css">

  
<script src="/myblog/fancybox/jquery.fancybox.pack.js"></script>




<script src="/myblog/js/script.js"></script>




  </div>
</body>
</html>